<template>
  <div class="dealerApplyDetails">
    <div class="header-box">
      <div class="title">经销商申请详情</div>
      <div class="btns">
        <el-button icon="CircleClose" round @click="doClose">关闭</el-button>
        <el-button v-if="dealerApplyInfo.applyStatus === 0" icon="Check" round type="success" @click="toExamine(true)">申请通过</el-button>
        <el-button v-if="dealerApplyInfo.applyStatus === 0" icon="Close" round color="#F95505" @click="toExamine(false)">申请驳回</el-button>
      </div>
    </div>
    <div class="content">
      <el-card shadow="never">
        <template #header>
          <el-icon color="#ffffff"><Tickets /></el-icon>
          <span class="title">经销商信息</span>
        </template>
        <template #default>
          <div class="info">
            <div><span>经销商编号：</span><span>{{ dealerApplyInfo.dealerCode }}</span></div>
            <div>
              <span>经销商类型：</span>
              <el-tag v-if="dealerApplyInfo.category === '1'" type="success">门店进销商</el-tag>
              <el-tag v-if="dealerApplyInfo.category === '2'" type="success">区域运营中心</el-tag>
              <el-tag v-if="dealerApplyInfo.category === '3'" type="success">市运营中心</el-tag>
              <el-tag v-if="dealerApplyInfo.category=== '4'" type="success">省运营中心</el-tag>
            </div>
            <div>
              <span>所属区域：</span>
              <span>{{ dealerApplyInfo.provinceName + dealerApplyInfo.cityName + dealerApplyInfo.regionName }}</span>
            </div>
            <div style="color: #F95505;">
              <span>审核状态：</span>
              <span v-if="dealerApplyInfo.applyStatus === 0">待审核</span>
              <span v-if="dealerApplyInfo.applyStatus === 1">通过</span>
              <span v-if="dealerApplyInfo.applyStatus === 2">驳回</span>
            </div>
          </div>
        </template>
      </el-card>
      <el-card shadow="never">
        <template #header>
          <el-icon color="#ffffff"><User /></el-icon>
          <span class="title">身份信息</span>
        </template>
        <template #default>
          <div class="info">
            <div><span>真实姓名：</span><span>{{ dealerApplyInfo.dealerName }}</span></div>
            <div><span>身份证号：</span><span>{{ dealerApplyInfo.idCard }}</span></div>
            <div><span>联系电话：</span><span>{{ dealerApplyInfo.phone }}</span></div>
            <div>
              <span>身份证附件：</span>
              <upload-img v-if="dealerApplyInfo.idCardInfo" v-model="dealerApplyInfo.idCardInfo" width="100" height="70" useScan></upload-img>
            </div>
            <div v-if="dealerApplyInfo.signImg">
              <span>电子签名：</span>
              <span><el-image :src="dealerApplyInfo.signImg" :preview-src-list="[dealerApplyInfo.signImg]" preview-teleported/></span>
            </div>
          </div>
        </template>
      </el-card>
      <el-card shadow="never">
        <template #header>
          <el-icon color="#ffffff"><Document /></el-icon>
          <span class="title">申请条件</span>
        </template>
        <template #default>
          <el-table :data="dealerApplyInfo.applyRuleList" :border="false">
            <el-table-column label="条件项" prop="ruleType">
              <template #default="{row}">
                <span>{{ $getDictValueByKey(row.ruleType, ruleTypes) + '：' + row.condition }}</span>
              </template>
            </el-table-column>
            <el-table-column label="是否满足" prop="achieveFlag" align="right">
              <template #default="{row}">
                <el-tag :type="row.achieveFlag ? 'success' : 'danger'" effect="dark">{{ row.status ? '已满足' : '未满足' }}</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-card>
    </div>
  </div>
</template>

<script setup name="dealerApplyDetails">
  import { getDealerApplyInfoApi, updateDealerApplyStatusApi } from '@/api/api'
  import { ref, getCurrentInstance, watch, computed } from 'vue'
  import { useStore } from 'vuex' 

  const store = useStore()

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties

  // 定义父组件传入的值
  const props = defineProps({
    dealerApplyId: {
      type: String,
      default: ''
    }
  })
  // 定义传值给父组件数据方法
  const emits = defineEmits(['close'])

  let dealerApplyInfo = ref({
    applyStatus: '',
    dealerCode: '',
    category: '',
    provinceName: '',
    cityName: '',
    regionName: '',
    dealerName: '',
    idCard: '',
    phone: '',
    idCardFront: '',
    idCardBack: '',
    idCardInfo: '',
    signImg: '',
    applyRuleList: []
  })

  let form = ref({
    dealerApplyId: '',
    applyStatus: '',
    rejectReason: ''
  })

  // 获取缓存页面数据
  let ruleTypes = computed(() => {
    return store.getters.getDictByKey('DEALER_RULE_TYPE')
  })

  watch(() => props.dealerApplyId, (newVal) => {
    init()
  }, {
    immediate: true // 立即监听
  })

  function init() {
    getDealerApplyInfoApi(props.dealerApplyId).then(res => {
      if(res.code === '200') {
        dealerApplyInfo.value = res.data
        dealerApplyInfo.value.idCardInfo = res.data.idCardFront + ',' + res.data.idCardBack
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 审核通过或驳回弹窗确认
  function toExamine(e) {
    form.value = {dealerApplyId: props.dealerApplyId, applyStatus: e ? 1 : 2, rejectReason: ''}
    if(e) {
      currentThis.$messageBox.confirm('确认通过吗？').then(() => {
        doExamine()
      }).catch(() => {})
    } else {
      currentThis.$messageBox.prompt('驳回原因', '确认驳回吗？').then(({value}) => {
        form.value.rejectReason = value
        doExamine()
      }).catch(() => {})
    }
  }
  // 执行审核
  function doExamine() {
    const loading = currentThis.$loading('正在处理，请耐心等待')
    updateDealerApplyStatusApi(form.value).then(res => {
      loading.close()
      if(res.code === '200') {
        doClose()
        currentThis.$message.success(res.message)
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  function doClose() {
    emits('close')
  }
</script>

<style lang="less" scoped>
.dealerApplyDetails{
  .header-box{
    height:8vh;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title{
      font-size: 25px;
      font-weight: bold;
    }
    .btns{
      display: flex;
      justify-content: flex-end;
    }
  }
  .content{
    height: 92vh;
    overflow: hidden auto;
    :deep(.el-card){
      margin-bottom: 15px;
      .el-card__header{
        display: flex;
        align-items: center;
        background: #3BB9F1;
        .el-icon{
          margin-right: 5px;
        }
        .title{
          color: #ffffff;
        }
      }
      .el-card__body{
        .info{
          display: flex;
          flex-wrap: wrap;
          >div{
            width: 33.3%;
            padding: 10px 0;
            color: #333333;
            font-size: 14px;
            display: flex;
            align-items: center;
          }
        }
        .el-image{
          width: 100px;
          height: 60px;
          border-radius: 8px;
          margin-right: 5px;
        }
      }
    }
  }
}
</style>